<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        /*
        手写bind方法
        1.定义函数
        2.this并调用原函数
        3.接受剩余参数并返回结果
        4.调用Symbol调优
        */
        Function.prototype.myBind = function (context, ...args) {
            return (...args2) => {
                let key = Symbol('key')
                context[key] = this
                const res = context[key](args, args2)
                delete context[key]
                return res
            }
        }
        function fn(name, age) {
            console.log(this)
            console.log(name, age)
            return name + age
        }
        const person = {
            name: 'why'
        }
        const bindFn = fn.myBind(person, 20)
        const res = bindFn(18)
        console.log('--返回值为--' + res)
    </script>
</body>

</html>